﻿using System;
using System.Windows.Threading;
using Infrastructure.Events;
using Infrastructure.ViewModels;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Unity;

namespace DocumentCommentModule.Views
{
    /// <summary>
    /// Document comment presenter
    /// </summary>
    public class DocumentCommentPresenter : IDocumentCommentPresenter
    {
        IUnityContainer _container;
        readonly IEventAggregator _eventAggregator;
        /// <summary>
        /// Gets or sets the view.
        /// </summary>
        /// <value>The view.</value>
        public IDocumentCommentView View { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="DocumentCommentPresenter"/> class.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="eventAggregator">The event aggregator.</param>
        /// <param name="view">The view.</param>
        public DocumentCommentPresenter(IUnityContainer container, IEventAggregator eventAggregator, IDocumentCommentView view)
        {
            _container = container;
            _eventAggregator = eventAggregator;
            View = view;

            DocumentSelectedEvent documentSelectedEvent = _eventAggregator.GetEvent<DocumentSelectedEvent>();
            documentSelectedEvent.Subscribe(DocumentSelectedEventHandler, true);

            BookshelfChangedEvenet bookshelfChangedEvenet = _eventAggregator.GetEvent<BookshelfChangedEvenet>();
            bookshelfChangedEvenet.Subscribe(BookshelfChangedEvenetHandler, true);
        }

        /// <summary>
        /// Bookshelf changed evenet handler.
        /// </summary>
        /// <param name="bookshelf">The new bookshelf.</param>
        void BookshelfChangedEvenetHandler(Bookshelf bookshelf)
        {
            // Set editor's enabled state
            View.Dispatcher.BeginInvoke(
                DispatcherPriority.Normal, 
                (Action<Bookshelf>)delegate(Bookshelf bs)
                                                   {
                                                       View.Editor.IsEnabled = (bs != null);
                                                   }, 
                bookshelf);
        }

        /// <summary>
        /// Documents the selected event handler.
        /// </summary>
        /// <param name="catalogItemVM">The catalog item VM.</param>
        void DocumentSelectedEventHandler(CatalogItemViewModel catalogItemVM)
        {
            // Set editor's text
            if (catalogItemVM != null)
            {
                View.Model = catalogItemVM;

                if (!string.IsNullOrEmpty(catalogItemVM.Comment))
                {
                    View.Document = catalogItemVM.Comment;
                }
                else
                {
                    View.Document = string.Empty;
                }
            }
            else
            {
                View.Model = null;
                View.Document = string.Empty;
            }
        }

    }
}
